// To change this License template, choose Tools / Templates
// and edit Licenses / FanDefaultLicense.txt
//
// History:
//   08.09.2010 JSTAUDEMEYER Creation
//

using [java]javax.sound.midi
using [java]fanx.interop::ByteArray

**
** SequenceDumper
**
class SequenceDumper
{

    Void show(Obj obj,Str[] fields ) {
        echo(Type.of(obj).name+": "+fields.map { obj.trap(it,[,])}.join("/"))
    }

  Void dump (Sequence sequence) {
    show(sequence,["getDivisionType","getResolution","getTickLength","getMicrosecondLength","getPatchList"])
    Track[] tracks := sequence.getTracks
    for (trackNo:=0; trackNo<tracks.size; trackNo++) {
            track := tracks[trackNo]
      echo("**** TRACK #$trackNo: size:${track.size} ticks:${track.ticks} *****")
      for (i:=0; i<track.size; i++) {
        event := track.get(i)
        msg := event.getMessage
        tick := event.getTick
        Env.cur.out.print(tick.toLocale("000000")+": ")
        if (msg is ShortMessage) {
          show(msg,["getCommand","getChannel","getData1","getData2"])
        } else if (msg is MetaMessage) {
          show(msg,["getType","getData"])
        } else if (msg is SysexMessage) {
          show(msg,["getData"])
        } else {
          throw Err("Unknown MIDI message type: "+Type.of(msg));
        }
      }
    }
  }
}